<?php
/**
 * bll task
 * @package app-drama_lib_bll
 */
load_lib('/bll/base');
load_lib('/dao/taskdao');
load_lib('/util/file');
/**
 * bll task
 * @author pandajingjing
 * @package app-drama_lib_bll
 */
class bll_task extends bll_base{

    /**
	 * 处理过的消息数
	 * @var int
	 */
    private $_intMsgCnt=0;

    function __construct(){
        parent::__construct();
    }

    /**
	 * 处理Drama的消息队列
	 */
    function doDramaMsg(){
        dao_taskdao::consumeMessage(array( 
                $this,
                'processMsg' 
        ));
        return $this->_intMsgCnt;
    }

    /**
	 * 队列消息处理逻辑
	 * @param mix $p_mixMsg
	 */
    function processMsg($p_mixMsg){
        $arrParam=$p_mixMsg->getBody();
        switch($arrParam['MSGTYPE']){
            case 1: //文件导入系统
                $this->importFile($arrParam);
                break;
            case 2: //节目新增剧集
                $this->addEp($arrParam);
                break;
            case 3: //发送剧集
                $this->sendEp($arrParam);
        }
        ++$this->_intMsgCnt;
        $p_mixMsg->fade();
    }

    /**
     * 发送剧集
     * @param array $p_arrParam
     */
    function sendEp($p_arrParam){
        $arrParam=array( 
                'QID'=>$p_arrParam['QUEUEID'],
                'DSTATUS'=>2 
        );
        dao_taskdao::updQueue($arrParam);
        util_file::tryCopyFile($p_arrParam['FROMPATH'],$p_arrParam['DESTPATH']);
        $arrParam=array(
                'QID'=>$p_arrParam['QUEUEID'],
                'DSTATUS'=>3
        );
        dao_taskdao::updQueue($arrParam);
    }

    /**
     * 节目新增剧集
     * @param array $p_arrParam
     */
    function addEp($p_arrParam){
        load_lib('/dao/episodedao');
        load_lib('/dao/seriesdao');
        $arrEpList=dao_episodedao::getEpList(array( 
                'SERIESID'=>$p_arrParam['SERIESID'],
                'DSTATUS'=>1 
        ));
        $intEpSize=$intEpCnt=0;
        $arrDownload=array();
        foreach($arrEpList as $arrEp){
            $intEpSize+=$arrEp['FILESIZE'];
            $intEpCnt++;
            $arrDownload[]=$arrEp['CREATETIME'];
        }
        $intDownload=max($arrDownload);
        dao_seriesdao::saveSeries(array( 
                'SERID'=>$p_arrParam['SERIESID'],
                'EPCNT'=>$intEpCnt,
                'EPSIZE'=>$intEpSize,
                'DOWNLOADTIME'=>$intDownload 
        ));
    }

    /**
	 * 导入文件
	 * @param array $p_arrParam
	 * @throws Exception
	 * @return boolean
	 */
    function importFile($p_arrParam){
        $arrParam=array( 
                'QID'=>$p_arrParam['QUEUEID'],
                'DSTATUS'=>2 
        );
        dao_taskdao::updQueue($arrParam);
        $strFileKey=$p_arrParam['FILEKEY'];
        $arrImportFile=dao_taskdao::getImportFile($strFileKey);
        if(null===$arrImportFile){
            $strDir=dirname($p_arrParam['DESTPATH']);
            if(!is_dir($strDir)){
                if(false===util_file::tryMakeDir($strDir,0755,true)){
                    throw new Exception(__CLASS__.': can not create path('.$strDir.').');
                    return false;
                }
            }
            $strDestFile=$strDir.$strFileKey;
            util_file::tryMoveFile($p_arrParam['FROMPATH'],$p_arrParam['DESTPATH']);
            $arrParam=array( 
                    'FILENAME'=>basename($p_arrParam['FROMPATH']),
                    'FILEKEY'=>$strFileKey,
                    'HOSTID'=>$p_arrParam['HOSTID'],
                    'FILESIZE'=>$p_arrParam['SIZE'] 
            );
            dao_taskdao::addImportFile($arrParam);
            $arrParam=array( 
                    'QID'=>$p_arrParam['QUEUEID'],
                    'DSTATUS'=>3 
            );
            dao_taskdao::updQueue($arrParam);
        }else{
            $arrParam=array( 
                    'QID'=>$p_arrParam['QUEUEID'],
                    'DSTATUS'=>5 
            );
            dao_taskdao::updQueue($arrParam);
        }
        if(file_exists($p_arrParam['FROMPATH'])){
            util_file::tryDeleteFile($p_arrParam['FROMPATH']);
        }
    }

    /**
	 * 扫描文件
	 * @return int
	 */
    function scanFile(){
        $arrImportPath=dao_taskdao::getConfig('path','import');
        $arrImportFile=array();
        foreach($arrImportPath as $strImportPath){
            $arrSubImportFile=util_file::tryReadDir($strImportPath,true);
            $arrImportFile=array_merge($arrImportFile,$arrSubImportFile);
        }
        foreach($arrImportFile as $arrFilePath){
            $strFilePath=$arrFilePath['PATH'];
            $strFileKey=md5_file($strFilePath);
            $intHostID=dao_taskdao::getConfig('storageid','storage');
            $strSubDir=$this->dispatchFile($strFileKey);
            $strBaseDir=dao_taskdao::getConfig('storage','storage');
            $strDir=$strBaseDir.DIRECTORY_SEPARATOR.$intHostID.$strSubDir;
            $strDestFile=$strDir.$strFileKey;
            $intFileSize=filesize($strFilePath);
            $arrQueue=array( 
                    'QTYPE'=>1,
                    'QDATA'=>json_encode(array( 
                            'FROMPATH'=>$strFilePath,
                            'SIZE'=>$intFileSize,
                            'DESTPATH'=>$strDestFile 
                    )) 
            );
            $intQueueID=dao_taskdao::addShowQueue($arrQueue);
            $arrMsg=array( 
                    'MSGTYPE'=>1,
                    'FROMPATH'=>$strFilePath,
                    'FILEKEY'=>$strFileKey,
                    'QUEUEID'=>$intQueueID,
                    'HOSTID'=>$intHostID,
                    'DESTPATH'=>$strDestFile,
                    'SIZE'=>$intFileSize 
            );
            dao_taskdao::addDramaQueue($arrMsg);
        }
        return count($arrImportFile);
    }

    /**
	 * 获取导入文件列表
	 * @return array
	 */
    function getQueueList(){
        $arrRS=dao_taskdao::getQueueList(array( 
                'SORT'=>'QID desc' 
        ));
        clearstatcache();
        foreach($arrRS as $intIndex=>$arrData){
            $arrQData=&$arrRS[$intIndex]['QDATA'];
            $arrQData=json_decode($arrQData,true);
            if(file_exists($arrQData['DESTPATH'])){
                $arrQData['FINISHSIZE']=filesize($arrQData['DESTPATH']);
            }else{
                $arrQData['FINISHSIZE']=-1;
            }
            $arrQData['FILENAME']=basename($arrQData['FROMPATH']);
        }
        //print_r($arrRS);
        return $arrRS;
    }

}